import requests
# 1、create  列表
# faceListId
faceListId = "shili04" #学生填写
create_facelists_url = 'https://apiiiiiiiii.cognitiveservices.azure.com/face/v1.0/facelists/{}' #学生填写
subscription_key = 'f12a79a82cf34cfbad67d1e7d2b2a0f4'#学生填写
assert subscription_key

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': subscription_key,
}
data = {
        "name": "上API课",
        "userData": "star",
        "recognitionModel": "recognition_02",
# 学生填写
    
}

r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data) #学生填写
r_create.content
# 已经成功创建facelist（云端/云计算）
#先加一张脸试试
# 2、Add face
add_face_url =  "https://apiiiiiiiii.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces"#学生填写

assert subscription_key
headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': subscription_key,
}
img_url ="http://l_shi_li.gitee.io/api-stores-things/kobe.jpg" #学生填写

params_add_face={
    "userData":"科比"
#学生填写
}

r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={"url":img_url})#学生填写
r_add_face.status_code
r_add_face.json()#返回persistedFaceId
# 封装成函数方便添加图片
def AddFace(img_url=str,userData=str):
    add_face_url ="https://apiiiiiiiii.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces"
    subscription_key = 'f12a79a82cf34cfbad67d1e7d2b2a0f4'#学生填写
    assert subscription_key
    headers = {
        # Request headers
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': subscription_key,
    }
    img_url = img_url

    params_add_face={
        "userData":userData
    }

r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={"url":img_url})

return r_add_face.status_code#返回出状态码

AddFace("http://l_shi_li.gitee.io/api-stores-things/fbb.jpg","范冰冰")
AddFace("http://l_shi_li.gitee.io/api-stores-things/baby.jpg","杨颖")
AddFace("http://l_shi_li.gitee.io/api-stores-things/hb.jpg","黄渤")
AddFace("http://l_shi_li.gitee.io/api-stores-things/kobe.jpg","科比")
AddFace("http://l_shi_li.gitee.io/api-stores-things/yyp.jpg","岳云鹏")

get_facelist_url = "https://apiiiiiiiii.cognitiveservices.azure.com/face/v1.0/facelists/{}"#学生填写
r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers,params=data)#学生填写
r_get_facelist.json()


# 3、检测人脸的id
# replace <My Endpoint String> with the string from your endpoint URL
face_api_url = 'https://apiiiiiiiii.cognitiveservices.azure.com/face/v1.0/detect'

# 请求正文
image_url = 'http://l_shi_li.gitee.io/api-stores-things/kobe.jpg'

headers = {'Ocp-Apim-Subscription-Key': subscription_key}

# 请求参数
params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    # 选择model
    'recognitionModel':'recognition_02',#此参数需与facelist参数一致
    'detectionModel':'detection_01',
    # 可选参数,请仔细阅读API文档
    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
}

response = requests.post(face_api_url, params=params,
                         headers=headers, json={"url": image_url})
# json.dumps 将json--->字符串
response.json()
findsimilars_url = "https://apiiiiiiiii.cognitiveservices.azure.com/face/v1.0/findsimilars"

# 请求正文 faceId需要先检测一张照片获取
data_findsimilars = {
    "faceId":"c71a66f9-50ff-49c5-ba91-cd417ef0762c",#取上方的faceID
    "faceListId": "shili04",
    "maxNumOfCandidatesReturned":10,
    "mode": "matchFace"#matchPerson #一种为验证模式，一种为相似值模式
    }

r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)
r_findsimilars.json()
import pandas as pd
#facelist里面的数据
adf = pd.json_normalize(r_get_facelist.json()["persistedFaces"])# 升级pandas才能运行
adf
#得到一个数据列表

# 返回相似度的数据
bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行
bdf
#得到一个数据列表

#合并在一起，得出班级能谁最像你
pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by="confidence",ascending = False)
#得到一个数据列表
